﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenBlock : MonoBehaviour {
    private Mesh mesh;
    private List<Vector3> vertices = new List<Vector3> ();
    private List<int> triangles = new List<int> ();

    // Use this for initialization
    void Start () {
        mesh = new Mesh ();

        AddFrontFace (0, 0, 0);
        AddBackFace (0, 0, 0);
        AddLeftFace (0, 0, 0);
        AddRightFace (0, 0, 0);
        AddUpFace (0, 0, 0);
        AddBottomFace (0, 0, 0);

        mesh.vertices = vertices.ToArray ();
        mesh.triangles = triangles.ToArray ();

        mesh.RecalculateBounds ();
        mesh.RecalculateNormals ();

        GetComponent<MeshFilter> ().mesh = mesh;
    }

    IEnumerator power (int num, int exponent) {
        int result = 1;
        for (int i = 0; i < exponent; i++) {
            result = result * num;
            yield return result;
        }
    }

    private void AddFrontFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord, yCoord, zCoord));
        vertices.Add (new Vector3 (xCoord, yCoord, zCoord + 1));
        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord + 1));
        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord));
    }

    private void AddBackFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord + 1));
        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord + 1));
    }

    private void AddLeftFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord));
        vertices.Add (new Vector3 (xCoord, yCoord, zCoord));
        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord));
    }

    private void AddRightFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord, yCoord, zCoord + 1));
        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord + 1));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord + 1));
        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord + 1));
    }

    private void AddUpFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord));
        vertices.Add (new Vector3 (xCoord, yCoord + 1, zCoord + 1));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord + 1));
        vertices.Add (new Vector3 (xCoord - 1, yCoord + 1, zCoord));
    }

    private void AddBottomFace (int xCoord, int yCoord, int zCoord) {
        setTri (triangles, vertices.Count);

        vertices.Add (new Vector3 (xCoord, yCoord, zCoord + 1));
        vertices.Add (new Vector3 (xCoord, yCoord, zCoord + 0));
        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord));
        vertices.Add (new Vector3 (xCoord - 1, yCoord, zCoord + 1));
    }

    private void setTri (List<int> tri, int index) {
        //第一个三角形
        tri.Add (0 + index);
        tri.Add (3 + index);
        tri.Add (2 + index);
        //第二个三角形
        tri.Add (2 + index);
        tri.Add (1 + index);
        tri.Add (0 + index);
    }
    // Update is called once per frame
    void Update () {

    }
}